package com.huang.config;

import com.huang.utils.WebUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import java.util.HashMap;
import java.util.Map;

/*
* 设置用户登录页面的
* */
@Configuration
public class WebSecutiryConfig extends WebSecurityConfigurerAdapter {
   /* @Bean
    public PasswordEncoder passwordEncoder(){

        return new BCryptPasswordEncoder();
    }*/
  //自定义登录页面
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    //设置用户登录yemain
     http.csrf().disable();
     http.formLogin()
            // .failureHandler(failureHandler());
            //我们自己的登录yemain
             .loginPage("/login.html")
             .loginProcessingUrl("/login")
            // .usernameParameter("username")
            // .passwordParameter("password")
             //.successHandler(new RedirectAuthenticationSuccessHandler("index.html"))
             .failureHandler(new RedirectAuthenticationFailureHandler("/login.html"))
             .defaultSuccessUrl("/index.html")
             .failureUrl("/login.html?error");
      //没有登陆的异常
    /* http.exceptionHandling()
             .authenticationEntryPoint(entryPoint());*/

     //设置页面用户登录的认证
        http.authorizeRequests()
                .antMatchers("/login.html")
                .permitAll()
                .anyRequest()
                .authenticated();

/*
           //
         http.exceptionHandling()
                 .authenticationEntryPoint(new DefaultAuthenticationEntryPoint())
                 .accessDeniedHandler(new DefaultAccessDeniedExceptionHandler());
*/

        //设置退出的
        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login.html?logout");
    }
    /**登录失败的处理器*/
    private AuthenticationSuccessHandler successHandler(){
       return (  request,   response,authentication ) -> {
           Map<String,Object> map=new HashMap<>();
           map.put("state",200);
           map.put("message","登录成功");
           WebUtils.writeJsonToClient(response,map);

           };

    }
    /**登录失败的处理器*/
    private AuthenticationFailureHandler failureHandler(){
        return ( request, response,e) -> {

            Map<String,Object> map=new HashMap<>();
            map.put("state",201);
            map.put("message","登录失败");
            WebUtils.writeJsonToClient(response,map); };

    }
    /**假如没有登录访问资源时给出提示*/
    private AuthenticationEntryPoint entryPoint(){
        return (request,response,exception)->{
            Map<String,Object> map=new HashMap<>();
            map.put("state",500);
            map.put("message","请先登录");
            //思考除了返回这些信息,还要返回什么?(JWT令牌)
            WebUtils.writeJsonToClient(response,map);

        };
    }

}
